﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.Model;
using System.Collections.Generic;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.Account.BLL;
using Yipusoft.Common.Model;

public partial class BasicBase_RoadSource_NewRoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //BindCityList("13");
        if (!Page.IsPostBack)
        {
            BindProvince();
            BindCities(ddlProvinces.SelectedValue);
            // BindDepartmentList();
            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200913") || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200900"))
                {
                    RoadInfo roadDetails = Road.GetRoadByID(Request["ID"]);
                    txtRoad.Text = roadDetails.RoadName;
                    txTrend.Text = roadDetails.Trend;
                    txtStartPoint.Text = roadDetails.StartPointName;
                    txtEndPoint.Text = roadDetails.EndPointName;
                    txtLength.Text = roadDetails.Length.ToString();
                    txtWidth.Text = roadDetails.Width.ToString();
                    ddlRank.SelectedValue = roadDetails.Rank.ToString();

                    if (roadDetails.RegionID != "")
                    {

                        CodeItemInfo iteminfo = CodeItem.GetCodeItem(roadDetails.RegionID);
                        if (iteminfo.ItemLevel == 2)
                        {
                            ddlProvinces.SelectedValue = iteminfo.ParentID;
                        }
                        else if (iteminfo.ItemLevel == 3)
                        {
                            ddlProvinces.SelectedValue = CodeItem.GetCodeItem(iteminfo.ParentID).ParentID;
                        }


                        //ddlProvinces.SelectedValue = CodeItem.GetCodeItem(roadDetails.RegionID).ItemLevel <= 2 ? CodeItem.GetCodeItem(roadDetails.RegionID).ParentID : CodeItem.GetCodeItem(CodeItem.GetCodeItem(roadDetails.RegionID).ParentID).ParentID;
                        BindCities(ddlProvinces.SelectedValue);
                        ddlCities.SelectedValue = roadDetails.RegionID;
                    }
                    string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
                    if (roadDetails.DepartmentID == departmentid || departmentid.Substring(0, 6) == roadDetails.DepartmentID.Substring(0, 4) + "01" || "130001" == departmentid.Substring(0, 6))
                    {
                    }
                    else
                    {
                        btnSave.Enabled = false;
                        lblInfo.Text = "只有" + roadDetails.DepartmentName + "单位和支队司令部内的用户方可编辑。";
                    }
                }
                else
                {
                    Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                    return;
                }

            }
            ViewState["PageIndex"] = 0;
            BindRoads();
        }
    }

    private void BindRoads()
    {

        gvRoads.DataSource = Road.GetRoadsList("SetTime>='" + DateTime.Today.ToString("yyyy-MM-dd") + "'", "SetTime", "Desc", 8, 0);
        gvRoads.DataBind();
    }


    private void BindProvince()
    {
        //ddlProvinces.DataSource = CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID");
        //ddlProvinces.DataTextField = "ItemName";
        //ddlProvinces.DataValueField = "ItemID";
        //ddlProvinces.DataBind();
        ListItem blankLI = new ListItem("", "");
        ddlProvinces.Items.Add(blankLI);
        foreach (CodeItemInfo item in CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID"))
        {
            ListItem li = new ListItem(item.ItemName, item.ItemID);
            ddlProvinces.Items.Add(li);
        }
    }

    protected void ddlProvinces_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(ddlProvinces.SelectedValue);
    }

    private void BindCities(string province)
    {
        //ddlCities.DataSource = CodeItem.GetList("ParentID='"+province+"' Order by SeqID");
        //ddlCities.DataTextField = "ItemName";
        //ddlCities.DataValueField = "ItemID";
        //ddlCities.DataBind();
        ddlCities.Items.Clear();
        if (province.Trim() == "")
        {
            ListItem blankLI = new ListItem("", "");
            ddlCities.Items.Add(blankLI);
        }
        else
        {
            foreach (CodeItemInfo item in CodeItem.GetList("ParentID='" + province + "' Order by SeqID"))
            {
                ListItem li = new ListItem(item.ItemName, item.ItemID);
                ddlCities.Items.Add(li);
                foreach (CodeItemInfo subItem in CodeItem.GetList("ParentID='" + item.ItemID + "' Order by SeqID"))
                    ddlCities.Items.Add(new ListItem("\u3000" + subItem.ItemName, subItem.ItemID));
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        decimal length = Convert.ToDecimal(txtLength.Text);
        if (length >= 10000)
        {
            lblInfo.Text = "提示：道路长度不能大于10000。";
            return;
        }

        string[] cc = txtLength.Text.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

        if (cc.Length > 1 && cc[1].Length > 2)
        {
            lblInfo.Text = "提示：道路长度小数长度不可大于2。";
            return;
        }

        decimal width = Convert.ToDecimal(txtWidth.Text);
        if (width >= 1000)
        {
            lblInfo.Text = "提示：道路宽度不能大于1000。";
            return;
        }

        string[] ccc = txtWidth.Text.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

        if (ccc.Length > 1 && ccc[1].Length > 2)
        {
            lblInfo.Text = "提示：道路宽度小数长度不可大于2。";
            return;
        }


        if (string.IsNullOrEmpty(Request["ID"]))
        {
            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200911") || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200912") || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "200900"))
            {
                RoadInfo roadDetails = new RoadInfo();
                roadDetails.RoadName = txtRoad.Text;
                roadDetails.Trend = txTrend.Text;
                roadDetails.StartPointName = txtStartPoint.Text;
                roadDetails.EndPointName = txtEndPoint.Text;
                roadDetails.Length = Convert.ToDecimal(txtLength.Text == "" ? "0" : txtLength.Text);
                roadDetails.Width = Convert.ToDecimal(txtWidth.Text == "" ? "0" : txtWidth.Text);
                roadDetails.Rank = Convert.ToDecimal(ddlRank.SelectedValue);
                roadDetails.RegionID = ddlCities.SelectedValue;
                roadDetails.DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
                if (Road.Insert(roadDetails) == 1)
                {
                    lblInfo.Text = "添加成功。";
                    BindRoads(ddlCities.SelectedValue, 8, 0);
                }
                else
                {
                    lblInfo.Text = "添加失败。";
                }
            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }
        else
        {
            RoadInfo roadDetails = Road.GetRoadByID(Request["ID"]);
            roadDetails.RoadName = txtRoad.Text;
            roadDetails.Trend = txTrend.Text;
            roadDetails.StartPointName = txtStartPoint.Text;
            roadDetails.EndPointName = txtEndPoint.Text;
            roadDetails.Length = Convert.ToDecimal(txtLength.Text == "" ? "0" : txtLength.Text);
            roadDetails.Width = Convert.ToDecimal(txtWidth.Text == "" ? "0" : txtWidth.Text);
            roadDetails.Rank = Convert.ToDecimal(ddlRank.SelectedValue);
            roadDetails.RegionID = ddlCities.SelectedValue;
            roadDetails.DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            if (Road.Update(roadDetails))
            {
                Response.Redirect("NewRoad.aspx");
            }
            else
            {
                lblInfo.Text = "修改失败。";
            }
        }
    }

    private void BindRoads(string regionID, int pageSize, int pageIndex)
    {
        // = Road.GetRoadsByRegionID(ddlCities.SelectedValue);
        if (!panPage.Visible)
            panPage.Visible = true;
        int totalCount = Road.GetRoadsByRegionID(ddlCities.SelectedValue).Count;
        //pageIndex = pageIndex + 1;
        ViewState["PageCount"] = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        if ((int)ViewState["PageCount"] < pageIndex)
            pageIndex = pageIndex - 1;
        ViewState["PageIndex"] = pageIndex;
        var query = (from r in Road.GetRoadsByRegionID(regionID)
                     orderby r.RegionID
                     select new
                     {
                         r.RoadID,
                         r.RoadName,
                         r.StartPointName,
                         r.EndPointName,
                         r.DepartmentName,
                         r.Trend,
                         r.Width,
                         r.Length,
                         r.RegionName
                     })
                                 .Skip(pageSize * pageIndex).Take(pageSize);

        gvRoads.DataSource = query;
        //hfTot.Value = Road.GetRoadsByRegionID(ddlCities.SelectedValue).Count.ToString();
        gvRoads.DataBind();
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    //protected void CityList_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    BindRoads(ddl);
    //}
    //protected void gvRoads_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    gvRoads.PageIndex = e.NewPageIndex;
    //    BindRoads();
    //}
    //protected void gvRoads_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.Pager)
    //    {
    //        Label lbGridInfo = e.Row.FindControl("lbGridInfo") as Label;
    //        lbGridInfo.Text = "共有<b>" + hfTot.Value + "</b>条记录 第<b>" + (gvRoads.PageIndex + 1).ToString() + "</b>页 共<b>" + gvRoads.PageCount.ToString() + "</b>页 每页<b>" + gvRoads.PageSize.ToString() + "</b>条";
    //    }
    //}
    protected void gvRoads_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        RoadInfo roadDetails = Road.GetRoadByID(gvRoads.DataKeys[e.RowIndex].Value.ToString());
        if (roadDetails.DepartmentID != Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID)
        {

            lblInfo.Text = "只有" + roadDetails.DepartmentName + "单位内的用户方可删除" + roadDetails.RoadName + "的数据。";
        }
        else
        {
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200900") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200914"))
            {
                if (Road.Delete(gvRoads.DataKeys[e.RowIndex].Value.ToString()))
                {
                    if (ddlCities.SelectedValue == "")
                        BindRoads();
                    else
                        BindRoads(ddlCities.SelectedValue, 8, (int)ViewState["PageIndex"]);
                }

            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }
    }

    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindRoads(ddlCities.SelectedValue, 8, int.Parse(JumpList.SelectedValue));
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindRoads(ddlCities.SelectedValue, 8, (int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindRoads(ddlCities.SelectedValue, 8, (int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindRoads(ddlCities.SelectedValue, 8, (int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindRoads(ddlCities.SelectedValue, 8, (int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void gvRoads_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * gvRoads.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}

